/*
 * @Description:
 * @Author: william
 * @Date: 2024-10-30 17:41:19
 * @LastEditors: william
 * @LastEditTime: 2024-10-30 18:23:23
 */

export const useBackPress = _ => {
  onMounted(() => {
    // 监听实体返回键
    if (typeof plus !== 'undefined') {
      plus.key.addEventListener('backbutton', handleBackPress)
    }
  })

  onUnmounted(() => {
    // 移除监听
    if (typeof plus !== 'undefined') {
      plus.key.removeEventListener('backbutton', handleBackPress)
    }
  })

  function handleBackPress() {
    console.log('Back button pressed')
    if (getCurrentPages().length > 1) {
      uni.navigateBack()
      return true
    } else {
      uni.navigateBack()
      return false
    }
  }

  return {}
}
